package com.pulaien.shop.controller;


import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.utils.SecurityUtils;
import com.pulaien.common.utils.StringUtils;
import com.pulaien.system.domain.ExchangeRate;
import com.pulaien.system.domain.TransactionRecord;
import com.pulaien.system.service.IExchangeRateService;
import com.pulaien.system.service.ITransactionRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * * 资金交易记录
 */
@RestController
@RequestMapping("/shop/transactionRecord")
@Api(tags = "资金交易记录")
public class ShopTransactionRecord {


    @Autowired
    private ITransactionRecordService transactionRecordService;

    @Autowired
    private IExchangeRateService exchangeRateService;

    /**
     * 资金交易记录
     * @return
     */
    @ApiOperation("资金交易记录")
    @GetMapping("getTransactionRecord")
    public AjaxResult getTransactionRecord(HttpServletRequest request){
        Long userId = SecurityUtils.getUserId();
        TransactionRecord transactionRecord = transactionRecordService.getTransactionRecord(userId);

        BigDecimal transactionTotalAmount = BigDecimal.ZERO;
        BigDecimal currentMonthTransactionAmount = BigDecimal.ZERO;
        BigDecimal rechargeTotalAmount = BigDecimal.ZERO;
        BigDecimal currentMonthRechargeTotalAmount = BigDecimal.ZERO;
        BigDecimal refundAmount = BigDecimal.ZERO;
        BigDecimal compensationAmount = BigDecimal.ZERO;
        if ( transactionRecord.getTransactionTotalAmount()!=null){
            transactionTotalAmount = transactionRecord.getTransactionTotalAmount();
        }

        if (transactionRecord.getCurrentMonthTransactionAmount() !=null){
            currentMonthTransactionAmount = transactionRecord.getCurrentMonthTransactionAmount();
        }

        if (transactionRecord.getRechargeTotalAmount()!=null){
            rechargeTotalAmount = transactionRecord.getRechargeTotalAmount();
        }

        if (transactionRecord.getCurrentMonthRechargeTotalAmount()!=null){
            currentMonthRechargeTotalAmount = transactionRecord.getCurrentMonthRechargeTotalAmount();
        }

        if (transactionRecord.getRefundAmount()!=null){
            refundAmount = transactionRecord.getRefundAmount();
        }

        if (transactionRecord.getCompensationAmount()!=null){
            compensationAmount = transactionRecord.getCompensationAmount();
        }

        String lang  = request.getHeader("Accept-Language");
        if (StringUtils.isNotBlank(lang) && ("ja-ja".equals(lang) || "en-us".equals(lang))){
            //获得日元汇率
            ExchangeRate exchangeRate = exchangeRateService.queryJpyExchangeRate();
            if (exchangeRate!=null && exchangeRate.getExchangeRate()!=null){
                transactionRecord.setTransactionTotalAmount(transactionTotalAmount.multiply(exchangeRate.getExchangeRate()).setScale(0, BigDecimal.ROUND_UP));
                transactionRecord.setCurrentMonthTransactionAmount(currentMonthTransactionAmount.multiply(exchangeRate.getExchangeRate()).setScale(0, BigDecimal.ROUND_UP));
                transactionRecord.setRechargeTotalAmount(rechargeTotalAmount.multiply(exchangeRate.getExchangeRate()));
                transactionRecord.setCurrentMonthRechargeTotalAmount(currentMonthRechargeTotalAmount.multiply(exchangeRate.getExchangeRate()).setScale(0, BigDecimal.ROUND_UP));
                transactionRecord.setRefundAmount(refundAmount.multiply(exchangeRate.getExchangeRate()).setScale(0, BigDecimal.ROUND_UP));
                transactionRecord.setCompensationAmount(compensationAmount.multiply(exchangeRate.getExchangeRate()).setScale(0, BigDecimal.ROUND_UP));
            }
        }
      /*  else if (StringUtils.isNotBlank(lang) && "en-us".equals(lang)){
            //获得美元汇率
            ExchangeRate exchangeRate = exchangeRateService.queryUsdExchangeRate();
            if (exchangeRate!=null && exchangeRate.getExchangeRate()!=null){
                transactionRecord.setTransactionTotalAmount(transactionTotalAmount.multiply(exchangeRate.getExchangeRate()).setScale(2, BigDecimal.ROUND_UP));
                transactionRecord.setCurrentMonthTransactionAmount(currentMonthTransactionAmount.multiply(exchangeRate.getExchangeRate()).setScale(2, BigDecimal.ROUND_UP));
                transactionRecord.setRechargeTotalAmount(rechargeTotalAmount.multiply(exchangeRate.getExchangeRate()).setScale(2, BigDecimal.ROUND_UP));
                transactionRecord.setCurrentMonthRechargeTotalAmount(currentMonthRechargeTotalAmount.multiply(exchangeRate.getExchangeRate()).setScale(2, BigDecimal.ROUND_UP));
                transactionRecord.setRefundAmount(refundAmount.multiply(exchangeRate.getExchangeRate()).setScale(2, BigDecimal.ROUND_UP));
                transactionRecord.setCompensationAmount(compensationAmount.multiply(exchangeRate.getExchangeRate()).setScale(2, BigDecimal.ROUND_UP));
            }
        }*/
        return AjaxResult.success().put("data",transactionRecord);
    }




}
